import {
	createRouter,
	createWebHistory,
	createWebHashHistory,
} from "vue-router";
import { baseURL } from "@/utils/axios.js";
import { ElMessage } from "element-plus";
import FilesPage from '@/pages/files/index.vue';
import LoginPage from "@/pages/login/login.vue";
import VideoIndex from "@/pages/video/index.vue";
import ArticleIndex from "@/pages/article/index.vue";
import ArticleEditor from "@/pages/article/editor.vue";
import UserStores from "@/stores/user.js";

const router = createRouter({
	history: createWebHashHistory(), //createWebHistory(import.meta.env.BASE_URL),
	routes: [
		{ path: "/", component: FilesPage},
		{ path: "/login", component: LoginPage, name: "Login" },
		{ path: "/video/:id", component: VideoIndex, name: "Video", props: true },
		{ path: "/article", component: ArticleIndex, name: "Article"},
		{ path: "/editor", component: ArticleEditor, name: "Editor"},
	],
});
router.beforeEach(async (to, from) => {
	if (window.location.host === baseURL.substring(8)) {
		return;
	}
	if (to.name === "Login") {
		//localStorage.removeItem("webman_token");
		return;
	}
	const { user } = UserStores();

	if (typeof user.expires !== "number" || user.expires < new Date().getTime()) {
		return { name: "Login" };
	}
});
export default router;
